package bmptest.bmp.service.SysRole;

import bmptest.bmp.entity.SysRole;
import bmptest.bmp.mapper.SysRole.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

@Service
public class SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    String create_by_id;
    String create_time;

    private String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    @Autowired
    public RedisTemplate<String,String> redisTemplate;

    //获取对当前时间
    private String getCurrentDate(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String currentDate = formatter.format(date);
        return currentDate;
    }

    public List<SysRole> searchRole(String  username,  String begindate, String enddate){
        String newbegindate = begindate.replaceAll("-","");
        String newenddate = enddate.replaceAll("-","");
        BigDecimal newbegindatewithoutsymbol;
        BigDecimal newenddatewithoutsymbol;
        if(newenddate!=null&&!newenddate.equals("")){
            newbegindatewithoutsymbol =new BigDecimal(newbegindate);
        }else{
            newbegindatewithoutsymbol = new BigDecimal("0");
        }
        if(newenddate!=null&&!newenddate.equals("")){
            newenddatewithoutsymbol =new BigDecimal(newenddate);
        }else{
            newenddatewithoutsymbol = new BigDecimal("99999999999999");
        }
        List<SysRole> allsysrole ;
        List<SysRole> rightdatesysrole = new LinkedList<>();
        String symbol = "%";
        String role_name = symbol+username+symbol;
        allsysrole = sysRoleMapper.searchRoleR(role_name);
        for(int i=0;i<allsysrole.size();i++){
            SysRole sysRole = allsysrole.get(i);
            String createtimewithoutsymbol = sysRole.getCreate_time().replaceAll("-","");
            BigDecimal decimalcreatetimewithoutsymbol =new BigDecimal(createtimewithoutsymbol);
            if(decimalcreatetimewithoutsymbol.compareTo(newbegindatewithoutsymbol)>=0&&decimalcreatetimewithoutsymbol.compareTo(newenddatewithoutsymbol)<=0){
                rightdatesysrole.add(sysRole);
            }
        }
        return rightdatesysrole;
    }

    @Transactional
    public String addAll(SysRole sysRole){
        String isvalidate = "1";
        String validaterolename = sysRole.getRole_name();
        if(validaterolename.length()>8||validaterolename.length()<2){
            isvalidate = "0";
            return isvalidate;
        }else{
            String s = this.getUUID();
            if(s!=null&&!s.equals("")){
                sysRole.setRole_id(s);
            }
            redisTemplate.setKeySerializer(RedisSerializer.string());
            create_by_id = redisTemplate.opsForValue().get("uid");
            if(create_by_id!=null&&!create_by_id.equals("")){
                sysRole.setCreate_by_id(create_by_id);
            }
            create_time=this.getCurrentDate();
            if(create_time!=null&&!create_time.equals("")){
                sysRole.setCreate_time(create_time);
            }
            this.addSysRole(sysRole);
            return isvalidate;
        }
    }

    public Integer addSysRole(SysRole sysRole){
        return sysRoleMapper.addSysRole(sysRole);
    }

    public void deleteRole(String role_id){
        sysRoleMapper.deleteRole(role_id);
    }

    public String updateRole(SysRole sysRole){
        String isvalidate = "1";
        String validateusername = sysRole.getRole_name();
        if(validateusername.length()>8||validateusername.length()<2){
            isvalidate = "0";
            return isvalidate;
        }else{
            sysRoleMapper.updateRole(sysRole);
            return isvalidate;
        }
    }

}
